using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProjectileExplosion:Projectile {

	[SerializeField] AudioClip[] explosionSound;

	protected override void Start() {
		base.Start();

		collider=GetComponent<CircleCollider2D>();
	}

	protected override void Init() {
		base.Init();
		int radius = (int)stat.size+1;
		Vector2Int start = new Vector2Int(Mathf.FloorToInt(transform.position.x)-radius,Mathf.FloorToInt(transform.position.y)-radius);
		Vector2Int end = new Vector2Int(Mathf.FloorToInt(transform.position.x)+radius,Mathf.FloorToInt(transform.position.y)+radius);

		for(int x = start.x;x<=end.x;x++) {
			for(int y = start.y;y<=end.y;y++) {
				Vector2Int pos = new Vector2Int(x,y);
				if(Vector2.Distance(pos,transform.position)>radius) continue;
				if(!TilemapController.SolidAt(pos)) continue;
				TilemapController.DamageTile(pos,getDamageStat);
			}
		}

		AudioPlayer.PlayAudio(transform.position,explosionSound,stat.size);

	}

	protected override void UpdateHit() { }
	protected override void UpdatePosition() { }

	private void OnTriggerStay2D(Collider2D collision) {
		Mob other = collision.GetComponent<Mob>();
		if(!other) return;
		Vector2 originalForward = forward;
		if(CanHit(other)) {
			forward=other.position-(Vector2)transform.position;
			Vector2 position = collision.ClosestPoint(transform.position);
			HitMob(other,transform.position-other.transform.position,position);
		}
		forward=originalForward;
	}

	new CircleCollider2D collider;

	protected override void UpdateIfStuck() {
		stuck=false;
	}
}
